package com.fumihiko.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateTimeUtil {
	private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private static DateFormat df2 = new SimpleDateFormat("yyMMdd");
	private static DateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static DateFormat df4 = new SimpleDateFormat("yyyyMM");
	private static DateFormat df5 = new SimpleDateFormat("yyyyMMdd");
	private static DateFormat df6 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	private static DateFormat df7 = new SimpleDateFormat("yyyy-MM-dd HH");



	/*
	 * 将时间戳转换为时间
	 */
	public static String stampToDate(String s){
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long lt = new Long(s);
		Date date = new Date(lt);
		res = simpleDateFormat.format(date);
		return res;
	}

	/**
	 * 将时间yyyy-M-dd HH:mm:ss格式化成yyMM
	 *
	 * @param time
	 * @return
	 */
	public static String getTimeFrormatYYMM(String time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return df4.format(format.parse(time));
		} catch (ParseException e) { // TODO
		}
		return null;
	}
	/**
	 * 将时间yyyy-M-dd HH:mm:ss格式化成yyMM
	 *
	 * @param time
	 * @return
	 */
	public static String getTimeFrormatByInputFormat(String time, String formatStr) {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		try {
			return format.format(format.parse(time));
		} catch (ParseException e) { // TODO
		}
		return null;
	}


	public static String getFormatTime(Date time, String formatStr) {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(time);
	}


	/**
	 * 得到minute 分钟后的时间,以HH:mm:ss格式的字符串返回
	 *
	 * @return 以 HH:mm:ss 格式的字符串返回
	 */
	public static String getHMS(int minute) {
		Date d = new Date(System.currentTimeMillis() + minute * 60 * 1000);
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		return df.format(d);
	}

	/**
	 * 得到minute 分钟后的时间,以HH:mm:ss格式的字符串返回
	 *
	 * @return 以 HH:mm:ss 格式的字符串返回
	 */
	public static String getH() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("HH");
		return df.format(c.getTime());
	}

	/**
	 * 将时间yyyyMMddHHmmss格式化成yyyy-M-dd HH:mm:ss
	 *
	 * @param time
	 * @return
	 */
	public static String getTimeFrormat4(Long time) {
		Date d = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(d);
	}


	/**
	 * Get current time , return format 'yyyy-MM-dd hh:mm:ss'
	 *
	 * @return
	 */
	public static String getCurrentTime() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	public static String getCurrentTime1() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	/**
	 * Get current time , return format 'yyyyMMddHHmmss'
	 *
	 * @return
	 */
	public static String getCurrentTime2() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	/**
	 * Get current time , return format 'hh:mm:ss'
	 *
	 * @return
	 */
	public static String getCurrentTime3() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	public static String getCurrentTime4() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	/**
	 * Get current time , return format
	 * @return
	 */
	public static String getCurrentTime(String format) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat(format);
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	/**
	 * Get current time , return format 'yyyy-MM-dd'
	 *
	 * @return
	 */
	public static String getCurrentTimeYMD() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	/**
	 * Get currentSecond time , return format 'yyyy-MM-dd' 获取第二天的年月日
	 *
	 * @return
	 */
	public static String getCurrentTimeYMDSecond() {
		Calendar c = Calendar.getInstance();
		try {
			String s1 = getCurrentTimeYMD();
			c.setTime(df.parse(s1));
			c.add(Calendar.DAY_OF_WEEK, 1);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return df.format(c.getTime());
	}

	/**
	 * Get current time , return format 'HH:mm:ss'
	 *
	 * @return
	 */
	public static String getCurrentTimeHMS() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	// Get current time , return format 'yyyy-MM-dd'
	public static String getCurrentDate1() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	// Get current time , return format 'yyyy-MM-dd'
	public static String getCurrentDate2() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(c.getTime());
	}

	// Get today's date, return format 'yyyy-MM-dd'
	public static String getTodayDate() {
		Calendar c = Calendar.getInstance();
		return df.format(c.getTime());
	}

	// Get today's date, return format 'yyMMdd'
	public static String getTodayDate2() {
		Calendar c = Calendar.getInstance();
		return df2.format(c.getTime());
	}

	// Get yesterday's date, return format 'yyyy-MM-dd'
	public static String getYesterdayDate() {
		Calendar c = Calendar.getInstance();
		c.add(GregorianCalendar.HOUR_OF_DAY, -24);
		return df.format(c.getTime());
	}

	// For nearly 7 days of the start date and end date, return format
	// 'yyyy-MM-dd'
	public static String[] getServenDate() {

		Calendar c = Calendar.getInstance();
		String[] s = new String[2];
		c.add(GregorianCalendar.HOUR_OF_DAY, -6 * 24);
		s[0] = df.format(c.getTime());

		c = Calendar.getInstance();
		s[1] = df.format(c.getTime());
		return s;
	}

	// For nearly 3 months of the start date and end date, return format
	// 'yyyy-MM-dd'
	public static String[] getThreeMonthDate() {

		Calendar c = Calendar.getInstance();
		String[] s = new String[2];
		c.add(GregorianCalendar.HOUR_OF_DAY, -90 * 24);
		s[0] = df.format(c.getTime());

		c = Calendar.getInstance();
		s[1] = df.format(c.getTime());
		return s;
	}

	// For nearly 1 months of the start date and end date, return format
	// 'yyyy-MM-dd'
	public static String[] getOneMonthDate() {

		Calendar c = Calendar.getInstance();
		String[] s = new String[2];
		c.add(GregorianCalendar.HOUR_OF_DAY, -30 * 24);
		s[0] = df.format(c.getTime());

		c = Calendar.getInstance();
		s[1] = df.format(c.getTime());
		return s;
	}

	// Get last month of the start date and end date, return format 'yyyy-MM-dd'
	public static String[] getLastMonthDate() {
		Calendar c = Calendar.getInstance();
		Date d;
		c.add(Calendar.MONTH, -1);
		d = c.getTime();
		String lastmonth_thisday = df.format(d);
		String[] s = { lastmonth_thisday.substring(0, 8) + "01", getMonthLastDay(lastmonth_thisday) };
		return s;
	}

	// Access to the month of the start date and the date today, return format
	// 'yyyy-MM-dd'
	public static String[] getThisMonthDate() {
		String s = getTodayDate();
		return new String[] { s.substring(0, 8) + "01", s };

	}

	/**
	 * 获取今天相差N天的日期
	 *
	 * @return yyyy-MM-dd
	 */
	public static String getDate(int days) {
		Calendar c = Calendar.getInstance();
		c.add(GregorianCalendar.HOUR_OF_DAY, 24 * days);
		return df.format(c.getTime());
	}

	/**
	 * 获取今天相差N天的日期
	 *
	 * @return yyyyMMdd
	 */
	public static String getDateyyyyMMdd(int days) {
		Calendar c = Calendar.getInstance();
		c.add(GregorianCalendar.HOUR_OF_DAY, 24 * days);
		return df5.format(c.getTime());
	}

	/**
	 * 获取今天相差N天的日期
	 *
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getDate1(int days) {
		Calendar c = Calendar.getInstance();
		c.add(GregorianCalendar.HOUR_OF_DAY, 24 * days);
		return df3.format(c.getTime());
	}

	/**
	 * 得到minute 分钟后的时间,以yyyy-MM-dd HH:mm:ss 格式的字符串返回
	 *
	 * @return 以 HH:mm:ss 格式的字符串返回
	 */
	public static String getAfterTime(int minute) {
		Date d = new Date(System.currentTimeMillis() + minute * 60 * 1000);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(d);
	}

	/**
	 * 得到sec 秒后的时间,以yyyy-MM-dd HH:mm:ss 格式的字符串返回
	 *
	 * @return 以 HH:mm:ss 格式的字符串返回
	 */
	public static String getAfterTimeOfSecond(int sec) {
		Date d = new Date(System.currentTimeMillis() + sec * 1000);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(d);
	}

	/**
	 * 2个字符串格式时间减法得到秒
	 *
	 * @param time1
	 *            yyyy-MM-dd HH:mm:ss
	 * @param time2
	 *            yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static long getTimeMinusToSec(String time1, String time2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date now;
		try {
			now = df.parse(time1);
			Date date = df.parse(time2);

			long l = now.getTime() - date.getTime();
			return l / 1000;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return -1;
		/*
		 * long day=l/(24*60*60*1000);
		 *
		 * long hour=(l/(60*60*1000)-day*24);
		 *
		 * long min=((l/(60*1000))-day*24*60-hour*60);
		 *
		 * long s=(l/1000-day*24*60*60-hour*60*60-min*60);
		 */

		// System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
	}

	/**
	 * 2个字符串格式时间减法得到小时
	 *
	 * @param time1
	 *            yyyy-MM-dd HH:mm:ss
	 * @param time2
	 *            yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static long getTimeMinusToHour(String time1, String time2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date now;
		try {
			now = df.parse(time1);
			Date date = df.parse(time2);

			long l = now.getTime() - date.getTime();
			return l / (1000 * 60 * 60);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return -1;
	}

	/**
	 * 给定一个时间,在给定的时间上增加或者减去分钟数，以yyyy-MM-dd HH:mm:ss 格式的字符串返回
	 */
	/********************************************************************************************************************************************
	 * by chenyp  2014.10.24
	 * SimpleDateFormat  
	 * Calendar 
	 * 日期格式是不同步的. 非线程安全类 
     * 需要创建独立实例.
	 *******************************************************************************************************************************************/
	public static String getAfterTime(String date, int minute) {
		DateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(df3.parse(date));
			c.add(Calendar.MINUTE, minute);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return df3.format(c.getTime());
	}

	/**
	 * 将时间yyyy-M-dd HH:mm:ss格式化成yyyyMMddHHmmss
	 *
	 * @param time
	 * @return
	 */
	public static String getTimeFrormat(String time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
		SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			return format1.format(format.parse(time));
		} catch (ParseException e) { // TODO
		}
		return null;
	}

	/**
	 * 将时间yyyyMMddHHmmss格式化成yyyy-M-dd HH:mm:ss
	 *
	 * @param time
	 * @return
	 */
	public static String getTimeFrormat2(String time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
		try {
			return format1.format(format.parse(time));
		} catch (ParseException e) { // TODO
		}
		return null;
	}

	/**
	 * 将时间yyyyMMddHHmmss格式化成yyyy-M-dd
	 *
	 * @param time
	 * @return
	 */
	public static String getTimeFrormat3(String time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-M-dd");
		try {
			return format1.format(format.parse(time));
		} catch (ParseException e) { // TODO
		}
		return null;
	}


	/**
	 * 将CST时间格式转成标准时间格式
	 *
	 * @param strDate
	 *            "2013-01-08 14:58:27.0 CST"
	 * @return
	 */
	public static String getCommDateFromCSTDate(String strDate) {
		if (null == strDate || "".equals(strDate)) {
			return "";
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1;
		try {
			date1 = df.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return df.format(date1);
	}

	/**
	 * Get last month last day date, return format'YYYY-MM-DD'
	 */
	public static String getMonthLastDay(String special_month) {
		String latetime = null;
		int i, j;
		String starttime = special_month.substring(0, 8) + "28";
		for (i = 28, j = 1;; i++, j++) {
			latetime = getChangeDateByHour(starttime, j);
			if (Integer.parseInt(latetime.substring(8, 10)) == 1) {
				break;
			}
		}
		return special_month.substring(0, 8) + i;
	}

	public static String getChangeDateByHour(String currdate, int n) {
		String sday = null;
		try {
			Date time = df3.parse(currdate);
			Date changedate = new Date(time.getTime() + n  * 3600 * 1000);
			sday = df3.format(changedate);
		} catch (Exception e) {
			System.out.println("error : " + e.getMessage());
		}
		return sday;
	}

	/**
	 * 得到一个指定时间后n秒的时间
	 *
	 * @param n
	 * @return
	 */
	public static String getChangeTime(String spectime, int n) {
		String sday = null;
		try {
			Date time = df3.parse(spectime);
			Date changedate = new Date(time.getTime() + n * 1000);
			sday = df3.format(changedate);
		} catch (Exception e) {
			System.out.println("error : " + e.getMessage());
		}
		return sday;
	}

	/**
	 * 得到上月今天的日期
	 *
	 * @return
	 */
	public static String getLastMonthTodayDate() {
		Calendar c = Calendar.getInstance();
		Date d;
		c.add(Calendar.MONTH, -1);
		d = c.getTime();
		return df.format(d);
	}

	/**
	 * get date
	 *
	 * @param queryTimeType
	 *            // 1=today,2=yesterday,3=nearly 7 days,4=last month,5=this
	 *            month,6=user-defined
	 * @param queryStartDate
	 *            // query start date
	 * @param queryEndDate
	 *            // query enddate
	 * @return
	 */
	public static String[] getSelectDate(String queryTimeType, String queryStartDate, String queryEndDate) {
		String[] res = new String[2];
		String s;
		switch (Integer.parseInt(queryTimeType)) {
		case 1: // 1=����
			s = DateTimeUtil.getTodayDate();
			res[0] = s;
			res[1] = s;
			break;
		case 2:// 2=����
			s = DateTimeUtil.getYesterdayDate();
			res[0] = s;
			res[1] = s;
			break;
		case 3:// 3=��7��
			res = DateTimeUtil.getServenDate();
			break;
		case 4: // 4=�ϸ���
			res = DateTimeUtil.getLastMonthDate();
			break;
		case 5: // 5=����
			res = DateTimeUtil.getThisMonthDate();
			break;
		default: // 6=�Զ���
			res[0] = queryStartDate;
			res[1] = queryEndDate;
			break;
		}
		return res;
	}

	/**
	 * get date
	 *
	 * @param queryTimeType
	 *            // 1=today,2=yesterday,3=nearly 7 days,4=nearly 1
	 *            month,5=nearly 3 month,6=user-defined
	 * @param queryStartDate
	 *            // query start date
	 * @param queryEndDate
	 *            // query enddate
	 * @return
	 */
	public static String[] getSelectDate2(String queryTimeType, String queryStartDate, String queryEndDate) {
		String[] res = new String[2];
		String s;
		switch (Integer.parseInt(queryTimeType)) {
		case 1:
			s = DateTimeUtil.getTodayDate();
			res[0] = s;
			res[1] = s;
			break;
		case 2:
			s = DateTimeUtil.getYesterdayDate();
			res[0] = s;
			res[1] = s;
			break;
		case 3:
			res = DateTimeUtil.getServenDate();
			break;
		case 4:
			res = DateTimeUtil.getOneMonthDate();
			break;
		case 5:
			res = DateTimeUtil.getThreeMonthDate();
			break;
		default:
			res[0] = queryStartDate;
			res[1] = queryEndDate;
			break;
		}
		return res;
	}

	public static String parseDate(Date date,String format) {
		DateFormat definedf = new SimpleDateFormat(format);
		return definedf.format(date);
	}

	public static Date formatDate(String date) {
		try {
			return df.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException("日期转换错误");
		}
	}

	public static Date formatToDate(String date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		try {
			return df.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException("日期转换错误");
		}
	}

	/**
	 * 返回指定日期的前一天
	 * @return yyyy-MM-dd
	 */
	public static String getSpecifiedDayBefore(String specifiedDay){
		//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		Calendar c = Calendar.getInstance();
		Date date=null;

		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
		e.printStackTrace();
		}

		c.setTime(date);
		int day=c.get(Calendar.DATE);
		c.set(Calendar.DATE,day-1);

		String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}

	/**
	 * 返回指定日期的后一天
	 * @return yyyy-MM-dd
	 */
	public static String getSpecifiedDayAfter(String specifiedDay){
		//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		Calendar c = Calendar.getInstance();
		Date date=null;

		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
		e.printStackTrace();
		}

		c.setTime(date);
		int day=c.get(Calendar.DATE);
		c.set(Calendar.DATE,day+1);

		String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}

	/**
	 * 判断当前日期是否是在23点50分 到 次日0点05分 之间
	 * 是则返回true
	 * 否则返回false
	 * @param strDate 传入时间
	 */
	public static boolean getInCureTimes(String strDate) throws Exception{
			SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
	        Date myDate = null;
	        Date dateBegin = null;
	        Date dateEnd = null;
	        try {
	            myDate = sd.parse(strDate);
	            dateBegin = sd.parse("23:50:00");
	            dateEnd = sd.parse("00:05:00");
	        } catch (ParseException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        if(strDate.equals("00:00:00") || strDate.equals("23:50:00") || strDate.equals("00:05:00") || strDate.equals("23:59:59")){
	        	return true;
	        }
	        if(myDate.after(dateBegin) && myDate.before(sd.parse("23:59:59"))){
	        	return true;
	        }
	        if(myDate.after(sd.parse("00:00:00")) && myDate.before(dateEnd)){
	        	return true;
	        }
	        return false;
	}

	/**
	 * 2个字符串格式时间减法得到分钟
	 *
	 * @param time1
	 *            yyyy-MM-dd HH:mm:ss
	 * @param time2
	 *            yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static long getTimeMinusToMinute(String time1, String time2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date now;
		try {
			now = df.parse(time1);
			Date date = df.parse(time2);

			long l = now.getTime() - date.getTime();
			return l / (1000 * 60);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return -1;
	}

	/**
	 * 将时间yyyy-M-dd HH:mm:ss格式化成yyMMdd
	 *
	 * @param time
	 * @return
	 */
	public static String getTimeFormat(String time, String dateFormat1, String dateFormat2) {
		SimpleDateFormat format = new SimpleDateFormat(dateFormat1);
		SimpleDateFormat format1 = new SimpleDateFormat(dateFormat2);
		try {
			return format1.format(format.parse(time));
		} catch (ParseException e) { // TODO
		}
		return null;
	}

	/**
	 * 判断当前日期是否是在23点分 到 次日0点05分 之间
	 * 是则返回true
	 * 否则返回false
	 * @param strDate 传入时间
	 */
	public static boolean getZjydInCureTimes(String strDate) throws Exception{
			SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
	        Date myDate = null;
	        Date dateBegin = null;
	        Date dateEnd = null;
	        try {
	            myDate = sd.parse(strDate);
	            dateBegin = sd.parse("23:55:00");
	            dateEnd = sd.parse("00:30:00");
	        } catch (ParseException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        if(strDate.equals("00:00:00") || strDate.equals("23:55:00") || strDate.equals("00:30:00") || strDate.equals("23:59:59")){
	        	return true;
	        }
	        if(myDate.after(dateBegin) && myDate.before(sd.parse("23:59:59"))){
	        	return true;
	        }
	        if(myDate.after(sd.parse("00:00:00")) && myDate.before(dateEnd)){
	        	return true;
	        }
	        return false;
	}

	public static int compareDate(String dateStart, String dateEnd) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date dt1 = df.parse(dateStart);
			Date dt2 = df.parse(dateEnd);
			if (dt1.getTime() > dt2.getTime()) {
				System.out.println("dateStart 在dateEnd前");
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				System.out.println("dateStart在dateEnd后");
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 判断时间是否在某个时间段内
	 * @param s 需要判断的时间,形如23:30:00
	 * @param s1 时间段开始时间,形如22:30:00
	 * @param s2 时间段结束时间,形如06:00:00
	 * @return boolean
	 */
	public static boolean isInTimeRange(String s,String s1,String s2){
		int set = Integer.valueOf(s.replaceAll(":",""));
		int begin = Integer.valueOf(s1.replaceAll(":",""));
		int end = Integer.valueOf(s2.replaceAll(":",""));
		if (begin > end){
			return set < end || set > begin;
		} else {
			return set > begin;
		}
	}

	/*
	 * 将时间转换为时间戳
	 */
	public static Long dateToStamp(String s){
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long ts = date.getTime();
		return ts;
	}

	/**
	 * 时间戳转换成日期格式字符串
	 * @param seconds 精确到秒的字符串
	 * @return
	 */
	public static String timeStamp2Date(String seconds) {
		if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
			return "";
		}
		//df6.setTimeZone(TimeZone.getTimeZone("UTC"));
		return df6.format(new Date(Long.valueOf(seconds)));
	}
	
	/**
	 * 时间戳转换成日期格式字符串(年-月-日 时)
	 * @param seconds 精确到秒的字符串
	 * @return
	 */
	public static String timeStampYYYYMMDDHH(String seconds) {
		if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
			return "";
		}
		//df6.setTimeZone(TimeZone.getTimeZone("UTC"));
		return df7.format(new Date(Long.valueOf(seconds)));
	}
	
	/**
	 * 日期格式字符串转换成时间戳
	 * @param date_str 字符串日期
	 * @return
	 */
	public static String date2TimeStamp(String date_str){
		try {
			//df6.setTimeZone(TimeZone.getTimeZone("UTC"));
			return String.valueOf(df6.parse(date_str).getTime()/1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	

	/**
	 * 获取某年第一天日期
	 * @param year 年份
	 * @return Date
	 */
	public static String getYearFirst(String year){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		Date currYearFirst = calendar.getTime();
		return df.format(currYearFirst);
	}
	
	/**
	 * 获取某年最后一天日期
	 * @param year 年份
	 * @return Date
	 */
	public static String getYearLast(String year){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();
		return df.format(currYearLast);
	}
	
	   //获取这一年的日历
	 private static Calendar getCalendarFormYear(int year){
	        Calendar cal = Calendar.getInstance();
	        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);      
	        cal.set(Calendar.YEAR, year);
	        return cal;
	    }
	
    //获取某一年的某一周的周日日期
	 public static String getEndDayOfWeekNo(int year,int weekNo){
	        Calendar cal = getCalendarFormYear(year);
	        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
	        cal.add(Calendar.DAY_OF_WEEK, 6);
	        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
	               cal.get(Calendar.DAY_OF_MONTH);    
	    }

	 //获取某一年的某一周的周一日期
	 public static String getStartDayOfWeekNo(int year,int weekNo){
	        Calendar cal = getCalendarFormYear(year);
	        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
	        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
	               cal.get(Calendar.DAY_OF_MONTH);    
	        
	  }

	 /**
	  * 返回 是第几周
	  * @param today
	  * @return
	  */
	public static Integer getWeekNum(String today){
		//String today = "2013-01-14";  
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		Date date = null;  
		try {  
		    date = format.parse(today);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}  
		  
		Calendar calendar = Calendar.getInstance();  
		calendar.setFirstDayOfWeek(Calendar.MONDAY);  
		calendar.setTime(date);  
		  
		return calendar.get(Calendar.WEEK_OF_YEAR);  
	}
	
	/**
	 * 返回第几季
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String GetQuarterByDate(String date){
		if(date == ""|| "".equals(date)){
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date datePar = null;
		try {
			datePar = sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(datePar);
		
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		int mouth = datePar.getMonth()+1;
		
		if(mouth>=1 && mouth<=3){
			return year + "年第一季度";
		}else if(mouth>=4 && mouth<=6){
			return year + "年第二季度";
		}else if(mouth>=7 && mouth<=9){
			return year + "年第三季度";
		}else if(mouth>=10 && mouth<=12){
			return year + "年第四季度";
		}else{
			return "";
		}
	}

	public static void main(String[] args) throws Exception{
		//System.out.println(DateTimeUtil.dateToStamp("2021-01-01 00:00:00"));
		//System.out.println(DateTimeUtil.getMonthLastDay("2021-02"));
		//System.out.println(DateTimeUtil.getYearFirst("2021"));
		//System.out.println(DateTimeUtil.getStartDayOfWeekNo(2021,1));
		//System.out.println(DateTimeUtil.getEndDayOfWeekNo(2021,1));
		//System.out.println(DateTimeUtil.getWeekNum("2010-01-03 01:01:01"));
		System.out.println(DateTimeUtil.getTimeMinusToSec("2021-10-20 15:00:01","2021-10-20 15:00:00"));

	}
}

